package com.lhstack.config.proxy.abstracts.impl;

import com.lhstack.config.proxy.abstracts.AbstractProxyCallableStatement;
import lombok.SneakyThrows;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ProxyCallableStatement extends AbstractProxyCallableStatement {

    public ProxyCallableStatement(CallableStatement statement) {
        super(statement);
    }

    @SneakyThrows
    @Override
    public boolean execute() throws SQLException {
        return (boolean) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .apply(callableStatement::execute);
    }

    @Override
    @SneakyThrows
    public ResultSet executeQuery(String sql) throws SQLException {
        return (ResultSet) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .addMetadata("sql",sql)
                .apply(() -> callableStatement.executeQuery(sql));
    }

    @Override
    @SneakyThrows
    public int executeUpdate(String sql) throws SQLException {
        return (int) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .addMetadata("sql",sql)
                .apply(() -> callableStatement.executeUpdate(sql));
    }

    @Override
    @SneakyThrows
    public boolean execute(String sql) throws SQLException {
        return (boolean) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .addMetadata("sql",sql)
                .apply(() -> callableStatement.execute(sql));
    }

    @Override
    @SneakyThrows
    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        return (int) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .addMetadata("sql",sql)
                .addMetadata("autoGenerateKeys",autoGeneratedKeys)
                .apply(() -> callableStatement.executeUpdate(sql,autoGeneratedKeys));
    }

    @Override
    @SneakyThrows
    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
        return (int) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .addMetadata("sql",sql)
                .addMetadata("columnIndexes",columnIndexes)
                .apply(() -> callableStatement.executeUpdate(sql,columnIndexes));
    }

    @Override
    @SneakyThrows
    public int executeUpdate(String sql, String[] columnNames) throws SQLException {
        return (int) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .addMetadata("sql",sql)
                .addMetadata("columnNames",columnNames)
                .apply(() -> callableStatement.executeUpdate(sql,columnNames));
    }

    @Override
    @SneakyThrows
    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        return (boolean) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .addMetadata("sql",sql)
                .addMetadata("autoGeneratedKeys",autoGeneratedKeys)
                .apply(() -> callableStatement.execute(sql,autoGeneratedKeys));
    }

    @Override
    @SneakyThrows
    public boolean execute(String sql, int[] columnIndexes) throws SQLException {
        return (boolean) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .addMetadata("sql",sql)
                .addMetadata("columnIndexes",columnIndexes)
                .apply(() -> callableStatement.execute(sql,columnIndexes));
    }

    @Override
    @SneakyThrows
    public boolean execute(String sql, String[] columnNames) throws SQLException {
        return (boolean) obServerConsumer.buildMetaData().addMetadata("statement",callableStatement)
                .addMetadata("sql",sql)
                .addMetadata("columnNames",columnNames)
                .apply(() -> callableStatement.execute(sql,columnNames));
    }
}
